package com.sea.human.service.impl;

import com.codecrane.core.power.entity.PowerGroup;
import com.codecrane.core.power.entity.PowerUser;
import com.codecrane.core.power.service.PowerUserService;
import com.codecrane.core.util.PerformanceStatusEnum;
import com.sea.human.entity.Performance;
import com.sea.human.entity.PerformanceDetail;
import com.sea.human.entity.PerformanceRecord;
import com.sea.human.entity.PerformanceTask;
import com.sea.human.mapper.PerformanceMapper;
import com.sea.human.service.PerformanceDetailService;
import com.sea.human.service.PerformanceRecordService;
import com.sea.human.service.PerformanceService;
import com.sea.human.service.PerformanceTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class PerformanceServiceImpl implements PerformanceService {

    @Autowired
    private PerformanceMapper performanceMapper;
    @Autowired
    private PerformanceDetailService performanceDetailService;
    @Autowired
    private PerformanceRecordService performanceRecordService;
    @Autowired
    private PowerUserService powerUserService;
    @Autowired
    private PerformanceTaskService performanceTaskService;

    @Override
    public List<Performance> findStatusNumber(Performance performance) {
        return performanceMapper.findStatusNumber(performance);
    }

    @Override
    public List<Performance> findByCnd(Performance performance) {
        return performanceMapper.findByCnd(performance);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(PowerUser powerUser, PowerGroup powerGroup, String performanceDate) {
        Performance performance=new Performance();
        performance.setUserId(Integer.parseInt(powerUser.getId().toString()));
        performance.setUserName(powerUser.getUserRealname());
        performance.setGroupId(Integer.parseInt(powerGroup.getGroupPid().toString()));
        performance.setGroupName(powerGroup.getGroupName());
        performance.setPerformanceDate(performanceDate);
        int res = performanceMapper.insert(performance);
        performanceRecordService.save(performance.getId(),0,powerUser.getUserRealname(), PerformanceStatusEnum.getStatus(0));

        List<PerformanceTask> cultureList = performanceTaskService.findByCulture();
        if(cultureList.size()>0){
            for(PerformanceTask culture:cultureList){
                PerformanceDetail performanceDetail=new PerformanceDetail();
                performanceDetail.setPerformanceId(performance.getId());
                performanceDetail.setPerformanceTask(culture.getPerformanceTask());
                performanceDetail.setPerformanceSource(culture.getPerformanceSource());
                performanceDetail.setPerformanceCheck(culture.getPerformanceCheck());
                performanceDetail.setPerformanceWeight(culture.getPerformanceWeight());
                performanceDetail.setFlag(2);
                performanceDetailService.save(performanceDetail);
            }
        }
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int usave(Performance performance) {
        Integer performanceStatus = performance.getPerformanceStatus();
        if(null!=performanceStatus) {
            String performanceRemark = PerformanceStatusEnum.getStatus(performance.getPerformanceStatus());
            //提交绩效
            if (performanceStatus.equals(1)) {
                Integer total = performanceDetailService.findTotalWeight(performance.getId());
                if (null == total || !total.equals(100)) {
                    throw new RuntimeException("权重总和不足100,请及时修改绩效！");
                }
            }
            //审核通过
            if (performanceStatus.equals(2)) {

            }
            //退回
            if (performanceStatus.equals(11)) {
                performanceRemark += "原因:" + performance.getPerformanceRemark();
            }
            if (performanceStatus.equals(6)) {
                //上级打分

            }
            if (performanceStatus.equals(31)) {
                //人事打回
                performanceRemark += "原因:" + performance.getPerformanceRemark();
            }
            performanceRecordService.save(performance.getId(), performance.getPerformanceStatus(),
                    powerUserService.getCurrentUserFromShiro().getUserRealname(),
                    performanceRemark);

        }
        return performanceMapper.update(performance);
    }

    @Override
    public List<Performance> findGradeByUser(Performance performance) {
        return performanceMapper.findGradeByUser(performance);
    }

    @Override
    public int updateScore(Integer id) {
        return performanceMapper.updateScore(id);
    }

    @Override
    public Performance findById(Integer id) {
        return performanceMapper.findById(id);
    }

    @Override
    public Integer findNumberByDateStatus(Integer performanceStatus, String performanceDate) {
        return performanceMapper.findNumberByDateStatus(performanceStatus,performanceDate);
    }

    @Override
    public Integer findPerformanceNumber(String performanceDate) {
        return performanceMapper.findPerformanceNumber(performanceDate);
    }

}
